import '../src/assets/js/flexible.js'
import '../src/assets/js/directive'
import '../src/assets/css/base.css'
import Vue from 'vue'
import router from './router'
import Index from './index.vue'
import store from './store/index'
import tim from 'tim'
import TIM from 'tim-js-sdk'
import axios from 'axios'
let Base64 = require('js-base64').Base64
import md5 from 'js-md5';
import { getbaseTime } from './api/im/api'
import basePath from '@/api/api.js'
import wx from 'weixin-js-sdk'
import Avatar from '@/components/pages/avatar.vue'
Vue.component('avatar', Avatar)
import toast from '@/components/toast/index.js'
import Vconsole from 'vconsole';
new Vconsole();
Vue.use(toast)
window.tim = tim
window.TIM = TIM
window.store = store
Vue.prototype.$bus = new Vue() // event Bus 用于无关系组件间的通信。
Vue.prototype.tim = tim
Vue.prototype.TIM = TIM
Vue.prototype.$store = store
Vue.prototype.$md5 =md5
Vue.prototype.$Base64 =Base64
Vue.prototype.$wx =wx
//获取cookie
Vue.prototype.$getCookie = function (name) {
  // var result = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  // if (result != null) {
  //     return unescape(result[2]);
  // }
  // return null;
  return localStorage.getItem(name);
}
//获取cookie
Vue.prototype.$setCookie = function (name, value, expires, path, domain) {
  // var exp = new Date();
  // exp.setTime(exp.getTime() + expires * 24 * 60 * 60 * 1000);
  // document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
  localStorage.setItem(name,value)
};
//获取cookie
Vue.prototype.$removeCookie = function (name) {
  // var exp = new Date();
  // exp.setTime(exp.getTime() - 1);
  // var value = this.$getCookie(name);
  // if (value != null)
  //     document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
  localStorage.removeItem(name);
};
//js部分  在main.js中加以下代码
Vue.prototype.formatWord = function (val) {
  let v = val
  v = v.replace(/\r/g, '');
   v =  v.replace(/\n/g, '')
   console.log(v.replace(/\n/g, ''))
   return v
};
//axios请求-携带cookie
axios.defaults.withCredentials = true
// http request 拦截器
axios.interceptors.request.use(
  config => {
    if(config.method === 'post') {
      if(!config.data){
        config.data={}
      }
      if(config.data.encryptLevel==2){
        //Content-Type: application/x-www-form-urlencoded;charset=UTF-8
        let param = new URLSearchParams()
        delete config.data.encryptLevel;
        for(var key in config.data){
          param.append(key, config.data[key])
        }
        config.data = param
      }else if(config.data.encryptLevel==3){
        //Content-Type: multipart/form-data
        let param = new FormData()
        delete config.data.encryptLevel;
        for(var key in config.data){
          param.append(key, config.data[key])
        }
        config.data = param
      }else if(config.data.encryptLevel==1){
        //接口加密请求
        config.headers.encryptLevel = Base64.encode(1);
        var paramsjson={encryptData: Base64.encode(JSON.stringify(config.data))}
        var time = getbaseTime()
        config.headers.encryptLevelSign = md5('mjk=encryptLevel=1='+time);
        config.data=paramsjson
      }else{
        // delete config.data.encryptLevel;
        // config.headers.encryptLevel = Base64.encode(0);
      }
    } else if(config.method === 'get') {
      if(!config.params){
        config.params={}
      }
      if(config.params.encryptLevel==2){
        //Content-Type: application/x-www-form-urlencoded;charset=UTF-8
        let param = new URLSearchParams()
        delete config.params.encryptLevel;
        for(var key in config.params){
          param.append(key, config.params[key])
        }
        config.params = param
      }else if(config.params.encryptLevel==3){
        //Content-Type: multipart/form-data
        let param = new FormData()
        delete config.params.encryptLevel;
        for(var key in config.params){
          param.append(key, config.params[key])
        }
        config.params = param
      }else if(config.params.encryptLevel==1){
        config.headers.encryptLevel = Base64.encode(1);
        var paramsjson={encryptData: Base64.encode(JSON.stringify(config.params))}
        var time = getbaseTime()
        config.headers.encryptLevelSign = md5('mjk=encryptLevel=1='+time);
        config.params=paramsjson
      }else{
        // delete config.params.encryptLevel;
        // config.headers.encryptLevel = Base64.encode(0);
      }
    }
    if(config.url.indexOf('miaomore.com')>0){
      var cookiestr = 'gid=022d8ff0d0bc49df84d838e88719a97c797;appid=328501;plat=3;sver=6000;pn=miaoguanfang;mfo=Xiaomi;sys=android;sysver=;plat_h5=3;';
      if(basePath.ENV=='prod'){
        //正式环境
        cookiestr = 'gid=022d8ff0d0bc49df84d838e88719a97c797;appid=321901;plat=3;sver=6000;pn=miaoguanfang;mfo=Xiaomi;sys=android;sysver=;plat_h5=3;';
      }
      // config.headers['Authorization'] =  cookiestr;
    }else{
      //D端接口header里增加token
      // config.headers['token'] =  localStorage.getItem('htoken');
    }
    
    return config;
  },
  err => {
    return Promise.reject(err);
});
// http response 拦截器
axios.interceptors.response.use(response => {
  if(response.data.encryptRetData){
    response.data =  JSON.parse(Base64.decode(response.data.encryptRetData))
  }
  if (response.data.status === 30042) {
    if(response.config&&response.config.url.indexOf('im/healthMgr/middle/SSOLogin')>=0){
      Vue.prototype.$removeCookie('token')
      Vue.prototype.$removeCookie('profileId')
      Vue.prototype.$removeCookie('userID')
      Vue.prototype.$removeCookie('userSig')
      //调用cwi操作-token过期
      window.parent.postMessage({type: 3},'*');
    }else if(Vue.prototype.$getCookie('upExportType')!=1){
      store.dispatch('logout')
    }else if(Vue.prototype.$getCookie('upExportType')==1){
      Vue.prototype.$removeCookie('token')
      Vue.prototype.$removeCookie('profileId')
      Vue.prototype.$removeCookie('userID')
      Vue.prototype.$removeCookie('userSig')
      //调用cwi操作-token过期
      window.parent.postMessage({type: 3},'*');
    }
    return response.data;
  } else {
    return response.data;
  }
}, error => {
  return Promise.reject(error)
})
new Vue({
	router,
  render: h => h(Index)
}).$mount('#app')
